<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/25 0025
 * Time: 9:19
 */
namespace app\adminapi\controller;

use app\adminapi\model\Api;
use app\api\model\JWT;
use think\App;
use think\Controller;
use think\Db;
use think\helper\Hash;


/**
 * @title 其他设置
 * @description 接口说明
 */
class Other extends Api
{
    /**
     * @title 客户来源
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/other/client
     * @method POST
     * @param name:id type:int require:1 default: other: desc:1宴会预定2零点预定
     * @return list:列表@
     * @list id:id name:名称 type:1宴会预定2零点预定 is_hidden:1显示2隐藏
     * @return page:总页数
     */
    public function client()
    {
        $user = $this->user;
        if(!input('type')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $type = input('type');
        $where[] = ['type','=',$type];
        $where[] = ['hid','=',$user['hid']];
        $where[] = ['is_del','=',1];
        $list = db::name('source')->where($where)->field('name,id,type,is_hidden')->select();
        $data['list'] = $list;
        return $this->ajaxReturn(0,'成功',$data);
    }
     /**
     * @title 客户来源修改状态
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/other/change
     * @method POST
     * @param name:id type:int require:1 default: other: desc:id
     * 
     */
    public function change()
    {
        $user = $this->user;
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');
        $status = db::name('source')->where('id',$id)->value('is_hidden');
        if($status == 1){
            $up['is_hidden'] = 2;
            db::name('source')->where('id',$id)->update($up);
        }elseif($status == 2){
            $up['is_hidden'] = 1;
            db::name('source')->where('id',$id)->update($up);       
        }
        return $this->ajaxReturn(0,'成功');
    }
    /**
     * @title 修改客户来源获取数据
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/other/client_up
     * @method POST
     * @param name:id type:int require:1 default: other: desc:id
     * 
     * @return list:列表@
     * @list id:id name:名称 is_hidden:1显示2隐藏
     * 
     */
    public function client_up()
    {
        $user = $this->user;
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');
        $res = db::name('source')->where('id',$id)->field('name,id,is_hidden')->find();
        $data['list'] = $res;
        return $this->ajaxReturn(0,'成功',$data);
    }
        /**
     * @title 删除客户来源
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/other/client_del
     * @method POST
     * @param name:id type:int require:1 default: other: desc:id
     * 
     * @return list:列表@
     * @list id:id name:名称 is_hidden:1显示2隐藏
     * 
     */
    public function client_del()
    {
        $user = $this->user;
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');
        db::name('source')->where('id',$id)->update(['is_del'=>2]);
        return $this->ajaxReturn(0,'成功');
    }
      /**
     * @title 客户来源的保存与修改
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/other/client_save
     * @method POST
     * @param name:id type:int require:0 default: other: desc:id
     * @param name:name type:int require:1 default: other: desc:名称
     * @param name:is_hidden type:int require:1 default: other: desc:1显示2隐藏
     * @param name:type type:int require:0 default: other: desc:1宴会2零点
     * 
     * 
     */
    public function client_save()
    {
        $user = $this->user;
        if(!input('name')){
            return $this->ajaxReturn(1,'请输入名称');
        }
        if(!input('is_hidden')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');

        $up['name'] = input('name');
        $up['is_hidden'] = input('is_hidden');
        if($id){
            db::name('source')->where('id',$id)->update($up);
        }else{
            $up['type'] = input('type');
            $up['hid'] = $user['hid'];
            db::name('source')->insert($up);
        }
        return $this->ajaxReturn(0,'成功');
    }

    /**
     * @title 渠道来源统计导出
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/other/banquet_export
     * @method POST
     */
    public function banquet_export()
    {
        $user = $this->user;
        // 查询数据
        $where[] = ['hid','=',$user['hid']];

        $source = db::name('source')->where('hid',$user['hid'])->field('id,name')->select();
        $where[] = ['source','neq',0];
        // 获客数
        $scheduled = db::name('scheduled')->where($where)->field('id,source')->select();
        // 成交数
        $where[] = ['type','=',3];
        $scheduleds = db::name('scheduled')->where($where)->field('id,source')->select();

        $wheres[] = ['type','=',3];
        $wheres[] = ['hid','=',$user['hid']];
        $count = db::name('scheduled')->where($wheres)->count();
        // 获客数
        $newArr = $this->dataGroup($scheduled,'source');
        foreach ($newArr as $k => $v) {
            foreach ($source as $ks => $vs) {
                if($vs['id'] == $k){
                    $source[$ks]['num'] = count($v);
                }
            }
        }
        // 成交数
        $newArrs = $this->dataGroup($scheduleds,'source');
        foreach ($source as $ks => $vs) {
            foreach ($newArrs as $kn => $vn) {
                if($vs['id'] == $kn){
                    $source[$ks]['deal'] = count($vn);
                }
            }
        }
        foreach ($source as $ka => $va) {
            $source[$ka]['effective'] = $va['deal'] / $count * 100;
            $source[$ka]['effective'] = $source[$ka]['effective'] .'%'; 
        }

        // 设置表头信息（对应字段名,宽度，显示表头名称）
        $cellName = [
            ['id', 'auto', 'ID'],
            ['name', 'auto', '渠道名称'],
            ['num', 'auto', '获客数'],
            ['deal', 'auto', '成交数'],
            ['effective', 'auto', '有效率'],
        ];
        // 调用插件（传入插件名，[导出文件名、表头信息、具体数据]）
        plugin_action('Excel/Excel/export', ['渠道来源统计', $cellName, $source]);
    }
    protected function dataGroup(array $dataArr,string $keyStr)
    {
        $newArr=[];
        foreach ($dataArr as $k => $val) {    //数据根据日期分组
            $newArr[$val[$keyStr]][] = $val;
        }
        return $newArr;
    }
    /**
     * @title 酒店设置获取名称和图片
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/other/hotel
     * @method POST
     * 
     * @return list:列表@
     * @list id:id username:名称 avatar:头像
     */
    public function hotel()
    {
        $user = $this->user;
        $where[] = ['id','=',$user['id']];
        $list = db::name('user')->where($where)->field('username,id,avatar')->find();
        if($list){
            $list['avatar'] = get_file_path($list['avatar']);
        }
        $data['list'] = $list;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 酒店昵称和头像的修改
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/other/hotel_up
     * @method POST
     * @param name:hotel type:int require:1 default: other: desc:名称
     * @param name:avatar type:int require:1 default: other: desc:头像（上传图片返回id）
     * 
     */
    public function hotel_up()
    {
        $user = $this->user;
        $hotel = input('hotel');
        $avatar = input('avatar');
        $id = $user['id'];
        if($hotel){
            db::name('user')->where('id',$id)->update(['username'=>$hotel,'hotel'=>$hotel]);
        }
        if($avatar){
            db::name('user')->where('id',$id)->update(['avatar'=>$avatar]);
        }
        return $this->ajaxReturn(0,'成功');
    }
    /**
     * @title 酒店修改密码
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/other/hotel_pwd
     * @method POST
     * @param name:old type:int require:1 default: other: desc:旧密码
     * @param name:password type:int require:1 default: other: desc:密码
     * @param name:again_pwd type:int require:1 default: other: desc:再次输入密码
     * 
     * 
     */
    public function hotel_pwd()
    {
        $user = $this->user;
        $id = $user['id'];
        $old = input('old');
        $password = input('password');
        $again_pwd = input('again_pwd');
        if(!$old){
            return $this->ajaxReturn(1,'请输入旧密码');
        }
        if(!$password){
            return $this->ajaxReturn(1,'请输入密码');
        }
        if(!$again_pwd){
            return $this->ajaxReturn(1,'请再次输入密码');
        }
        if($password && $again_pwd){
            if(!$password != $again_pwd){
                return $this->ajaxReturn(1,'两次密码不对');
            }
        }
        $user = db::name('user')->where('id',$id)->find();
        if (!Hash::check((string)$password, $user['password'])) {
            return $this->ajaxReturn(1,'旧密码错误');
        } 
        if(db::name('user')->where('id',$id)->update(['password'=>$this->setPasswordAttr($password)])){
            return $this->ajaxReturn(0,'成功');
        }
    }
         // 对密码进行加密
    public function setPasswordAttr($value)
    {
        return Hash::make((string)$value);
    }
}